﻿using System;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Collections;
using System.Text;

namespace _0512063_0512082
{
    class SampleLoader
    {
        private static String samplePath = Application.StartupPath + "\\Samples\\";
        private DirectoryInfo sampleFolder;
        private Hashtable samples;
        public String SampleFolderPath
        {
            get { return samplePath; }
        }
        public SampleLoader(){
            samples = new Hashtable();
            sampleFolder = new DirectoryInfo(samplePath);
            if(!sampleFolder.Exists){
                sampleFolder.Create();
            }
            loadAllSamples();
        }

        /// <summary>
        /// (re)load all examples
        /// </summary>
        /// <returns>new example-names</returns>
        public String[] loadAllSamples(){
            List<String> names = new List<String>(); 
            FileInfo[] files = sampleFolder.GetFiles();
            for(int i = 0 ; i < files.Length; i++)
            {
                String key = files[i].Name.Replace(".txt","");
                if (samples[key] == null)
                {
                    names.Add(key);
                    samples.Add(key, files[i].FullName);
                }
            }
            return names.ToArray();
        }

        /// <summary>
        /// get array contains sample names
        /// </summary>
        public String[] getSampleName(){
            String[] names = new String[samples.Count];
            samples.Keys.CopyTo(names, 0);
            return names;
        }

        /// <summary>
        /// get samples content
        /// </summary>
        /// <param name="name">sample name</param>
        /// <returns></returns>
        public String getSample(String name){
            StreamReader sr = new StreamReader((String)samples[name]);
            String kq = sr.ReadToEnd();
            sr.Close();
            return kq;
        }

        public  static void saveSample(String content,String name)
        {
            StreamWriter sw = new StreamWriter(samplePath + name + ".txt");
            sw.Write(content);
            sw.Close();
        }
    }
}
